package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.dao.SaleListGoodsMapper;
import com.atguigu.jxc.dao.SaleListMapper;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.SaleList;
import com.atguigu.jxc.entity.SaleListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Security;
import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings("all")
public class SaleListGoodsServiceImpl extends ServiceImpl<SaleListGoodsMapper, SaleListGoods> implements SaleListGoodsService {

    @Autowired
    SaleListGoodsMapper saleListGoodsMapper;

    @Autowired
    SaleListMapper saleListMapper;

    @Autowired
    UserDao userDao;

    @Override
    public ServiceVO saveSaleListGoods(SaleList saleList, String saleListGoods) {

        int flag = 0;
        User user = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        saleList.setUserId(user.getUserId());
        flag += saleListMapper.insert(saleList);

        List<SaleListGoods> listGoods = JSON.parseArray(saleListGoods, SaleListGoods.class);

        for (SaleListGoods listGood : listGoods) {
            listGood.setSaleListId(saleList.getSaleListId());
            flag += saleListGoodsMapper.insert(listGood);

        }

        ServiceVO serviceVO;
        if(flag!=0){
            serviceVO = new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
        }else
            serviceVO = new ServiceVO(ErrorCode.REQ_ERROR_CODE,ErrorCode.REQ_ERROR_MESS,null);
        return serviceVO;
    }

    @Override
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<Map> list = saleListGoodsMapper.count(sTime, eTime, goodsTypeId, codeOrName);
        // System.out.println(list);
        // list.forEach(System.out::println);
        return JSON.toJSONString(list);
    }
}
